package com.codes.service.impl;

import com.codes.core.common.CConstant;
import com.codes.dao.LogisticsMapper;
import com.codes.dao.OrderInfoMapper;
import com.codes.dao.OrderInfoWMMapper;
import com.codes.dao.WorkOrderMapper;
import com.codes.model.WorkOrder;
import com.codes.service.MessageSV;
import com.codes.service.OrderInfoWMSV;
import com.codes.service.WorkOrderSV;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date 2020/5/26 10:25
 * @Created by peanut
 */
@Service
public class WorkOrderImpl implements WorkOrderSV {

    @Autowired
    private WorkOrderMapper workOrderMapper;
    @Autowired
    private OrderInfoMapper orderInfoMapper;
    @Autowired
    private LogisticsMapper logisticsMapper;
    @Autowired
    private MessageSV messageSV;
    @Autowired
    private OrderInfoWMMapper orderInfoWMMapper;
    @Autowired
    private OrderInfoWMSV orderInfoWMSV;


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(WorkOrder record) {
        return workOrderMapper.insert(record);
    }

    @Override
    public int insertSelective(WorkOrder record) {
        return workOrderMapper.insertSelective(record);
    }

    @Override
    public int insertByMap(Map m) {
        workOrderMapper.insertByMap(m);
        Integer workOrderId = MapUtils.getInteger(m, "id");
        //
        List<Map<String, Object>> listMap = (ArrayList) m.get("workOrderInfo");
        if (listMap.size() == 0) return 0;
        for (Map<String, Object> item : listMap) {
            item.put("workOrderId", workOrderId);
            orderInfoMapper.insertByMap(item);
        }
        return workOrderId;
    }

    @Override
    public int saveOrderDay(Map map) {
        int re = 0;
        re = workOrderMapper.insertByMap(map);
        Integer workOrderId = MapUtils.getInteger(map, "id");
        List<Map<String, Object>> wuliu = (ArrayList) map.get("logisticsInfo");

        // 获取所有物流单
        for (Map m : wuliu) {
            //  三车.    每车 有 多个商品,先保存物流单,然后保存物流单详情, 然后发消息给司机
            m.put("workOrderId", workOrderId);
            re = logisticsMapper.insertByMap(m);
            Integer logisticsId = MapUtils.getInteger(m, "id");
            // 商品详情列表
            List<Map<String, Object>> productList = (ArrayList) m.get("productList");
            if (productList.size() > 0) {
                // 说明有商品  开始保存详情
                for (Map mm : productList) {
                    // 物流单保存后返回id 保存到详情表
                    mm.put("logisticsId", logisticsId);
                    mm.put("workOrderId", null);// 需求工单id 设置为null
                    re = orderInfoMapper.insertByMap(mm);
                }
            }
        }
        return re;
    }

    @Override
    public int saveOrderWeekMonth(Map map) {
        int re = 0;
        Integer workOrderId = MapUtils.getInteger(map, "workOrderId");
        Integer orderType = MapUtils.getInteger(map,"orderType");
        if(null == workOrderId){
            // save
            re = workOrderMapper.insertByMap(map);
            workOrderId =  MapUtils.getInteger(map, "id");
        }else {
            re = workOrderMapper.updateByPrimaryKeySelective(map);
            // 删除之前的  工单详情;
            re = orderInfoWMMapper.deleteByWorkOrderId(workOrderId);

        }
        // 重新添加 详情单
        List<Map<String, Object>> orderInfoWMList = (ArrayList) map.get("orderInfoWMList");
        if(null != orderInfoWMList && orderInfoWMList.size() > 0 ){
            for (Map<String, Object> m: orderInfoWMList) {
                //   workOrderId, type, productId, trainNo, startDate, endDate
                m.put("workOrderId",workOrderId);
                m.put("type",orderType);
                re = orderInfoWMMapper.insertSelective(m);
            }
            return re;
        }
        return re;
    }

    @Override
    public WorkOrder selectByPrimaryKey(Integer id) {
        return workOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Map map) {
        //
        int re = 0;
        Integer workOrderId = MapUtils.getInteger(map, "workOrderId");
        Integer workOrderState = MapUtils.getInteger(map, "state");
        if (null == workOrderId) return 0;
        re = workOrderMapper.updateByPrimaryKeySelective(map);
        if (0 != workOrderState) {
            // 工单不为0 则 修改   驳回 配送 完成 发送消息
            Map oMap = workOrderMapper.getById(workOrderId);
            Integer receiveUser = MapUtils.getInteger(oMap, "applyUser"); // 工单申请人变为消息接收人
            oMap.put("receiveUser", receiveUser);
            messageSV.insertSelective(oMap);
        }
        if (CConstant.WORKORDER_STATE_UPDATE == workOrderState || CConstant.WORKORDER_STATE_DAIAUDIT == workOrderState) {
            // 状态为 3 修改 则修改 工单详情  删除之前的工单详情 重新添加      还有为 0   申请人修改
            // 删除之前的 工单详情
            re = orderInfoMapper.deleteByWrokOrderId(workOrderId);
            List<Map<String, Object>> listMap = (ArrayList) map.get("workOrderInfo");
            if (listMap.size() == 0) re = 0;
            for (Map<String, Object> item : listMap) {
                item.put("workOrderId", workOrderId);
                orderInfoMapper.insertByMap(item);
            }
            return re;
        }
        return re;
    }

    @Override
    public int updateOrderDay(Map map) {
        //
        int re = 0;
        Integer workOrderId = MapUtils.getInteger(map, "workOrderId");
        Integer workOrderState = MapUtils.getInteger(map, "state");
        if (null == workOrderId) return 0;
        re = workOrderMapper.updateByPrimaryKeySelective(map);
        if (0 != workOrderState) {
            // 工单不为0 则 修改   驳回 配送 完成 发送消息
            Map oMap = workOrderMapper.getById(workOrderId);
            Integer receiveUser = MapUtils.getInteger(oMap, "applyUser"); // 工单申请人变为消息接收人
            oMap.put("receiveUser", receiveUser);
            messageSV.insertSelective(oMap);
        }
        if (CConstant.WORKORDER_STATE_UPDATE == workOrderState || CConstant.WORKORDER_STATE_DAIAUDIT == workOrderState) {
            // 状态为 3 修改 则修改 工单详情  删除之前的工单详情 重新添加      还有为 0   申请人修改
            // 删除之前的 物流工单详情
            re = orderInfoMapper.deleteByLogisticsId(workOrderId);
            //  同时删除 日计划相关的 物流单
            re = logisticsMapper.deleteByWorkOrderId(workOrderId);
            // 获取所有物流单
            List<Map<String, Object>> wuliu = (ArrayList) map.get("logisticsInfo");
            if (wuliu.size() > 0 ) {

                for (Map m: wuliu) {
                    //  三车.    每车 有 多个商品,先保存物流单,然后保存物流单详情, 然后发消息给司机
                    m.put("workOrderId",workOrderId);
                    re = logisticsMapper.insertByMap(m);
                    Integer logisticsId = MapUtils.getInteger(m,"id");
                    //  物流单保存玩, 发送消息给司机
                    Map oMap =  workOrderMapper.getById(workOrderId);
                    oMap.put("logisticsId",logisticsId);
                    Integer receiveUser = MapUtils.getInteger(m,"driverId");
                    oMap.put("receiveUser",receiveUser);
                    messageSV.insertSelective(oMap);
                    // 商品详情列表
                    List<Map<String,Object>> productList  = (ArrayList) m.get("logOrderInfo");
                    if(null != productList && productList.size() > 0){
                        // 说明有商品  开始保存详情
                        for ( Map mm: productList) {
                            // 物流单保存后返回id 保存到详情表
                            mm.put("logisticsId",logisticsId);
                            mm.put("workOrderId",null);// 需求工单id 设置为null
                            re =   orderInfoMapper.insertByMap(mm);
                        }
                    }
                }
            }


            return re;
        }
        return re;
    }

    @Override
    public int updateByPrimaryKey(WorkOrder record) {
        return 0;
    }

    @Override //  根据 工单id 获取 工单详情
    public Map getWrokOrderInfo(Map<String, Object> map) {
        Integer workOrderId = MapUtils.getInteger(map, "workOrderId");
        if (null == workOrderId) return null;
        WorkOrder w = workOrderMapper.selectByPrimaryKey(workOrderId);
        List<Map<String, Object>> infos = orderInfoMapper.selectByOrderId(map);
        map.put("workOrder", w);
        map.put("workOrderInfo", infos);
        return map;
    }

    @Override
    public List<Map<String, Object>> getList(Map<String, Object> map) {
        return workOrderMapper.getList(map);
    }

    @Override
    public Map<String, Object> getById(Integer id) {
        Map<String, Object> map = workOrderMapper.getById(id);
        // 详情  需求工单   put workOrderId
        map.put("workOrderId", id);
        List<Map<String, Object>> infos = orderInfoMapper.selectByOrderId(map);
        List<Map<String, Object>> logisticsInfo = logisticsMapper.getLogisticsByOrderId(map);
        for (Map m : logisticsInfo) {
            Integer logisticsId = MapUtils.getInteger(m, "logisticsId");
            if (null != logisticsId) {
                //  根据 物流单 获取 商品详情
                m.put("workOrderId", null);// 条件置空
                m.put("logisticsId", logisticsId);
                List<Map<String, Object>> logOrderInfo = orderInfoMapper.selectByOrderId(m);
                m.put("logOrderInfo", logOrderInfo);

            }
        }

        map.put("logisticsInfo", logisticsInfo);
        map.put("workOrderInfo", infos);
        return map;
    }

    @Override
    public Map<String, Object> getInfoWM(Integer id) {
        Map<String, Object> map = workOrderMapper.getById(id);
        Map m = new HashMap();
        m.put("workOrderId",id);
        List<Map<String,Object>> oiwmList = orderInfoWMSV.getListByOrderId(m) ;
        map.put("orderInfoWMList",oiwmList);
        return map;
    }

    @Override
    public int getCount(Map map) {
        return workOrderMapper.getCount(map);
    }

    @Override
    public List<Map<String, Object>> getListByRole(Map map) {
        return workOrderMapper.getListByRole(map);
    }

    @Override
    public int deleteById(Map m) {
        return workOrderMapper.updateByPrimaryKeySelective(m);
    }

    @Override
    public int updateByIdDel(Map map) {
        int re = 0 ;
        Integer workOrderId = MapUtils.getInteger(map,"workOrderId");
        // 删除工单  删除  wuliu  详  情
        Integer orderType = MapUtils.getInteger(map,"orderType");
        re = workOrderMapper.updateByIdDel(map);
        if( 0 == orderType){
            re =  orderInfoMapper.deleteByLogisticsId(workOrderId);
            re = logisticsMapper.deleteByWorkOrderId(workOrderId);
        }else {  //  1 周 2 月
            re = orderInfoWMMapper.deleteByWorkOrderId(workOrderId);
        }
        return re;
    }

    @Override
    public Map getInfoById(Map map) {
        return workOrderMapper.getInfoById(map);
    }

    @Override
    public List<Map<String, Object>> getListDayExport(Map m) {
        return workOrderMapper.getListDayExport(m);
    }

    @Override
    public int deleteBatch(List<String> ids) {
        int re = 0;
        Integer workOrderId;
        for (String id:ids             ) {
            workOrderId = Integer.parseInt(id);
            if(null != workOrderId){
                re =   workOrderMapper.deleteByPrimaryKey(workOrderId);
                re =  orderInfoMapper.deleteByLogisticsId(workOrderId);
                re = logisticsMapper.deleteByWorkOrderId(workOrderId);
            }
        }
        return re;
    }

    @Override
    public List<Map<String, Object>> getTotalList(Map map) {
        return workOrderMapper.getTotalList(map);
    }

    @Override
    public int getTotalListCount(Map map) {
        return workOrderMapper.getTotalListCount(map);
    }
}